/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.flowform.form.service;

import java.util.List;

import com.jeeplus.modules.flowform.form.entity.RepairMake;
import com.jeeplus.modules.flowform.form.entity.RepairMakeSafe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.flowform.form.entity.RepairPlan;
import com.jeeplus.modules.flowform.form.mapper.RepairPlanMapper;
import com.jeeplus.modules.flowform.form.entity.RepairPlanPttach;
import com.jeeplus.modules.flowform.form.mapper.RepairPlanPttachMapper;

/**
 * 材料准备Service
 * @author 王洋
 * @version 2020-11-10
 */
@Service
@Transactional(readOnly = true)
public class RepairPlanService extends CrudService<RepairPlanMapper, RepairPlan> {

	@Autowired
	private RepairPlanPttachMapper repairPlanPttachMapper;

	public RepairPlan get(String id) {
		RepairPlan repairPlan = super.get(id);
		repairPlan.setRepairPlanPttachList(repairPlanPttachMapper.findList(new RepairPlanPttach(repairPlan)));
		return repairPlan;
	}

	public List<RepairPlan> findList(RepairPlan repairPlan) {
		return super.findList(repairPlan);
	}

	public Page<RepairPlan> findPage(Page<RepairPlan> page, RepairPlan repairPlan) {
		return super.findPage(page, repairPlan);
	}
	public RepairPlan getId(RepairPlan repairPlan){
		return mapper.getId(repairPlan);
	}
	@Transactional(readOnly = false) // 如果不加这一行那么就只能只读不能修改所以用到修改方法是必加的
	public void updateState(RepairPlan repairPlan){
		mapper.updateState(repairPlan);
	}
	@Transactional(readOnly = false)
	public void save(RepairPlan repairPlan) {
		super.save(repairPlan);
		for (RepairPlanPttach repairPlanPttach : repairPlan.getRepairPlanPttachList()){
			if (repairPlanPttach.getId() == null){
				continue;
			}
			if (RepairPlanPttach.DEL_FLAG_NORMAL.equals(repairPlanPttach.getDelFlag())){
				if (StringUtils.isBlank(repairPlanPttach.getId())){
					repairPlanPttach.setRepairCoding(repairPlan);
					repairPlanPttach.preInsert();
					repairPlanPttachMapper.insert(repairPlanPttach);
				}else{
					repairPlanPttach.preUpdate();
					repairPlanPttachMapper.update(repairPlanPttach);
				}
			}else{
				repairPlanPttachMapper.delete(repairPlanPttach);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(RepairPlan repairPlan) {
		super.delete(repairPlan);
		repairPlanPttachMapper.delete(new RepairPlanPttach(repairPlan));
	}

}